package cn.com.cfae.iras.doc.analyze.components.consistency;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.PageModel;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by zouxun on 2018/10/29
 */
@Component("U1012")
@Scope("prototype")
public class U1012 extends BaseComponent {
    private static Logger logger = LoggerFactory.getLogger(U1012.class);

    @Autowired
    U1004 u1004;

    /**
     * @desc 首先请检查募集说明书中M-2是否含有“有息债务”字眼，清定位M-6-3及M-2中“有息债务”位置，由初评人员对照有息债务信息是否一致。
     * 1、如果M-2中含有息债务，比较有息债务总额；
     * 1.1如果不能抓取有息债务总额，进行提示
     * 1.2M-2、M-6-3不能抓取，分别进行提示
     * 2、M-2和M-6-3比对，提示在第二章
     * @return
     */
    @Override
    public List<ComponentResult> handlerM() {
        logger.info("开始调用U1012组件，处理M表......................");
        String comparisonCatalogId="M-6";
        List<ComponentResult> componentResults = ruleCheck(comparisonCatalogId);
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        List<ComponentResult> componentResults = new ArrayList<>();
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        logger.info("开始调用U1012组件，处理DM表......................");
        String comparisonCatalogId="DM-6";
        List<ComponentResult> componentResults = ruleCheck(comparisonCatalogId);
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return null;
    }


    private List<ComponentResult> ruleCheck(String comparisonCatalogId){
        DocumentModel documentModel = super.getExtractData();

        ChapterModel m2ChapterModel = documentModel.getChapterModel(getCatalogId());
        ChapterModel m6ChapterModel = documentModel.getChapterModel(comparisonCatalogId);

        List<ComponentResult> componentResults = new ArrayList<>();

        ComponentResult m2ComponentResult = u1004.getComponentResult(getCatalogId(),getCatalogId(),getRuleTitle(),getCatalogId().concat("有息债务"),"",false,getRegulationId(),0,null,"03");
        ComponentResult m6ComponentResult = u1004.getComponentResult(getCatalogId(),getCatalogId(),getRuleTitle(),comparisonCatalogId.concat("有息债务"),"",true,getRegulationId(),0,null,"03");

        try{
            if(m2ChapterModel!=null){
                List<PageModel> m2PageModelList = m2ChapterModel.getPageModelList();

                m2ComponentResult.setPageNum(m2ChapterModel.getStartPage());

                boolean flag = true;

                if(CollectionUtils.isNotEmpty(m2PageModelList)){
                    top:for(PageModel pageModel:m2PageModelList){
                        JsonArray jsonTextDataArray = pageModel.getJsonTextData()!=null?pageModel.getJsonTextData().getAsJsonArray():null;
                        if(jsonTextDataArray!=null){
                            for(JsonElement element : jsonTextDataArray){
                                JsonArray dataArray = element.getAsJsonObject()!=null?element.getAsJsonObject().getAsJsonArray("data"):null;//当页文本
                                if(dataArray!=null){
                                    for (int i = 0; i < dataArray.size(); i++) {
                                        JsonArray e = dataArray.get(i).getAsJsonArray();
                                        for (int y = 0; y < e.size(); y++) {
                                            JsonObject data = e.get(y).getAsJsonObject();
                                            String text = data.get("text").getAsJsonPrimitive().getAsString();
                                            Double top = data.get("top").getAsJsonPrimitive().getAsDouble();
                                            Double left = data.get("left").getAsJsonPrimitive().getAsDouble();
                                            if(text.contains("有息债务") || text.contains("付息债务")){
                                                m2ComponentResult.setPromptMessage(getCatalogId().concat("中存在有息债务相关内容，请人工核对"));
                                                m2ComponentResult.setPageNum(pageModel.getPageNumber());
                                                List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();
                                                ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
                                                textPosition.setContent(text);
                                                textPosition.setX(left);
                                                textPosition.setY(top);
                                                textPositionList.add(textPosition);
                                                m2ComponentResult.setTextPosition(textPositionList);
                                                flag=true;
                                                break top;
                                            }else{
                                                flag=false;
                                            }
                                        }

                                    }
                                }
                            }
                        }
                    }

                    if(!flag){
                        m2ComponentResult.setAnnotationFlag(true);
                        m2ComponentResult.setPromptMessage(getCatalogId().concat("中无有息债务相关内容"));
                    }else{
                        if(m6ChapterModel!=null){
                            boolean m6Flag = false;

                            List<PageModel> m6PageModelList = m6ChapterModel.getPageModelList();

                            m6ComponentResult.setPageNum(m6ChapterModel.getStartPage());

                            if(CollectionUtils.isNotEmpty(m6PageModelList)){
                                top:for(PageModel pageModel:m6PageModelList){
                                    JsonArray jsonTextDataArray = pageModel.getJsonTextData()!=null?pageModel.getJsonTextData().getAsJsonArray():null;
                                    if(jsonTextDataArray!=null){
                                        for(JsonElement element : jsonTextDataArray){
                                            JsonArray dataArray = element.getAsJsonObject()!=null?element.getAsJsonObject().getAsJsonArray("data"):null;//当页文本
                                            if(dataArray!=null){
                                                for (int i = 0; i < dataArray.size(); i++) {
                                                    JsonArray e = dataArray.get(i).getAsJsonArray();
                                                    for(int y=0;y<e.size();y++){
                                                        JsonObject data = e.get(y).getAsJsonObject();
                                                        String text = data.get("text").getAsJsonPrimitive().getAsString();
                                                        Double top = data.get("top").getAsJsonPrimitive().getAsDouble();
                                                        Double left = data.get("left").getAsJsonPrimitive().getAsDouble();
                                                        if(text.contains("有息债务情况") || text.contains("发行人有息债务") || text.contains("有息债务分析") || text.contains("付息债务")){
                                                            m6Flag = true;
                                                            m6ComponentResult.setPageNum(pageModel.getPageNumber());
                                                            List<ComponentResult.TextPosition> textPositionList = new ArrayList<>();
                                                            ComponentResult.TextPosition textPosition = new ComponentResult().new TextPosition();
                                                            textPosition.setContent(text);
                                                            textPosition.setX(left);
                                                            textPosition.setY(top);
                                                            textPositionList.add(textPosition);
                                                            m6ComponentResult.setTextPosition(textPositionList);
                                                            m6ComponentResult.setAnnotationFlag(false);
                                                            break top;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                if(m6Flag){
                                    m6ComponentResult.setPromptMessage(comparisonCatalogId.concat("中存在有息债务相关内容，请人工核对"));
                                }else{
                                    m6ComponentResult.setPromptMessage(comparisonCatalogId.concat("中未发现有息债务相关内容"));
                                }

                            }else{
                                m6ComponentResult.setPromptMessage(("未识别").concat(comparisonCatalogId).concat("数据"));
                            }
                        }else{
                            m6ComponentResult.setPromptMessage(("未识别").concat(comparisonCatalogId).concat("数据"));
                        }
                        componentResults.add(m6ComponentResult);

                    }

                    componentResults.add(m2ComponentResult);

                }else{
                    m2ComponentResult.setPromptMessage(("未识别").concat(getCatalogId()).concat("数据"));
                    componentResults.add(m2ComponentResult);
                }
            }else{
                m2ComponentResult.setPromptMessage(("未识别").concat(getCatalogId()).concat("数据"));
                componentResults.add(m2ComponentResult);
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return componentResults;
    }

}
